#ifndef TEXTURE2DMANIPULATOR_H
#define TEXTURE2DMANIPULATOR_H

#include "Log.h"
#include "Exception.h"
#include "Texture2D.h"
#include "Colors.h"

namespace Cvekas {

/// Allows to access individual pixels of Texture2D and manipulate them
class Texture2DManipulator : public boost::noncopyable
{
public:
	/// Constructor
	/// \param texture Pointer to texture
	/// \param use_buffer If true, then additional buffer in system memory is used to do all reads and writes.
	Texture2DManipulator(Texture2DPtr texture, bool use_buffer = true);
	/// Destructor
	virtual ~Texture2DManipulator();

	/// Sets pixel color.
	void setPixel(uint x, uint y, const Color& color);
	/// Returns pixel color.
	Color getPixel(uint x, uint y);
	/// Applies changes. After calling apply texture is ready to be used.
	void apply();

private:
	void lock();
	void unlock();

	Texture2DPtr texture;
	bool use_buffer;

	LockedTexture locked;
	uint height, pitch;

	byte* buffer;
};

typedef boost::shared_ptr<Texture2DManipulator> Texture2DManipulatorPtr;

} // namespace

#endif